package com.future.common.data.utils;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName : DesensitizationUtils
 * @Author : lby
 * @Date: 2020/11/18 15:09
 * @Description : 脱敏工具类
 */
public class DesensitizationUtils {
    /**
     * 公共字符串脱敏
     */
    public static String desValue(String origin, int prefixNoMaskLen, int suffixNoMaskLen, String maskStr) {
        if (origin == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0, n = origin.length(); i < n; i++) {
            if (i < prefixNoMaskLen) {
                sb.append(origin.charAt(i));
                continue;
            }
            if (i > (n - suffixNoMaskLen - 1)) {
                sb.append(origin.charAt(i));
                continue;
            }
            sb.append(maskStr);
        }
        return sb.toString();
    }

    /**
     * 姓名脱敏
     * **三
     */
    public static String chineseName(String fullName) {
        if (fullName == null) {
            return null;
        }
        return desValue(fullName, 0, 1, "*");
    }

    /**
     * 身份证号脱敏
     * 340304*******1234
     */
    public static String idCardNum(String id) {
        return desValue(id, 6, 4, "*");
    }

    /**
     * 手机号脱敏
     * 135****6810
     */
    public static String mobilePhone(String num) {
        return desValue(num, 3, 4, "*");
    }

    /**
     * 邮箱校验
     * d**@126.com
     */
    public static String email(String email) {
        if (email == null) {
            return null;
        }
        int index = StrUtil.indexOf(email, '@');
        if (index <= 1) {
            return email;
        }
        String preEmail = desValue(email.substring(0, index), 1, 0, "*");
        return preEmail + email.substring(index);

    }


    /**
     * 银行卡校验
     * 622260**********1234
     */
    public static String bankCard(String cardNum) {
        return desValue(cardNum, 6, 4, "*");
    }


}
